/**
 * GarinDriver -- Eclipse Public License - v 1.0
 * http://www.eclipse.org/legal/epl-v10.html
 */
package garin.parse.sql.sql2003;

import garin.parse.Token;
import garin.parse.io.Source;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * http://savage.net.au/SQL/sql-2003-2.bnf.html
 * 
 * @author Mark Farnsworth
 */
public class SqlStatement extends SqlToken {
	List<Token> children = new ArrayList<Token>();
	private final static String keywords = ResourceManager
			.get("/garin/parse/sql/sql2003/keywords.txt");
	
	/**
	 * @param p
	 *            Context for parsing
	 * @throws Exception
	 */
	public SqlStatement(final Source p) throws Exception {
		source(p);
	}
	
	@Override
	public void init() throws Exception {
		super.init();
		final StringTokenizer t = new StringTokenizer(keywords);
		while (t.hasMoreTokens())
			add(new StatementWord(this, t.nextToken()));
	}
	
	/**
	 * When we encounter / we enter into multilineComment mode (we will exit
	 * quickly if the / is not followed by a *)
	 * <p>
	 * Parsing the -- style comments is similar
	 * <p>
	 * When we hit a ';' the statement is over and we go up().
	 * 
	 * @throws Exception
	 */
	@Override
	public Token on() throws Exception {
		final Token t = super.on();
		if (t != parent()) children.add(t);
		return t;
	}
}
